package org.csu.jpetstoreagain.controller;

import com.alibaba.fastjson.JSON;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.csu.jpetstoreagain.domain.Category;
import org.csu.jpetstoreagain.domain.Inventory;
import org.csu.jpetstoreagain.domain.Item;
import org.csu.jpetstoreagain.domain.Product;
import org.csu.jpetstoreagain.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.http.HttpResponse;
import java.util.List;

@Controller
@RequestMapping("/catalog")
public class CatalogController {

    @Autowired   //(required = false)
    private CatalogService catalogService;

    @GetMapping("/view")
    public String view(Model model,HttpSession session) {
        List<Category> categoryList = catalogService.getCategoryList();
        session.setAttribute("categoryList",categoryList);
        return "jsp/catalog/main";
    }


    //找一个商品大类
    @GetMapping("/viewCategory")
    public String viewCategory(String categoryId, Model model) {  //拿参。model用来存放数据
        if (categoryId != null) {
            Category category = catalogService.getCategory(categoryId);
            List<Product> productList = catalogService.getProductListByCategory(categoryId);
            model.addAttribute("category", category);
            model.addAttribute("productList", productList);
            return "jsp/catalog/category";  //这里要返回一个地址,即跳转到的下一个界面
        }
        return "jsp/catalog/main";
    }


    //找一个product
    @GetMapping("/viewProduct")
    public String viewProduct(String productId, Model model,HttpSession session) {  //拿参。model用来存放数据
        //System.out.println("?");
        //session.setAttribute("searchErrorMSg","");
        if (productId != null) {
            Product product = catalogService.getProduct(productId);
            List<Item> itemList = catalogService.getItemListByProduct(productId);
            model.addAttribute("product", product);
            model.addAttribute("itemList", itemList);
            return "jsp/catalog/product";  //这里要返回一个地址,即跳转到的下一个界面
        }
        return "jsp/catalog/category";
    }


    @GetMapping("/viewItem")
    public String viewItem(String itemId, Model model) {  //拿参。model用来存放数据
        if (itemId != null) {
            Item item = catalogService.getItem(itemId);
            Product product = catalogService.getProduct(item.getProductid());
            List<Item> itemList = catalogService.getItemListByProduct(itemId);
            Inventory inventory = catalogService.getItemQuantityByItemId(itemId);
            model.addAttribute("item", item);
            model.addAttribute("product", product);
            model.addAttribute("inventory", inventory);
            return "jsp/catalog/item";  //这里要返回一个地址,即跳转到的下一个界面
        }
        return "jsp/catalog/product";
    }
    //注意上面这里可能要补一个 Product product=item.getProduct();session.setAttribute("product",product);


    @PostMapping("/viewSearch")
    public String viewSearch(String keywords, Model model,HttpSession session) {  //拿参。model用来存放数据
        System.out.println("enter");
        if (keywords != null&&!keywords.isEmpty()) {
            List<Product> productList = catalogService.searchProductList(keywords);
            model.addAttribute("productList", productList);
            //session.setAttribute("searchErrorMSg","");
            //System.out.println("234"+session.getAttribute("searchErrorMSg"));
            //System.out.println("1"+keywords);
            return "jsp/catalog/searchProducts";  //这里要返回一个地址,即跳转到的下一个界面
        }
        //System.out.println("2"+keywords);
        //session.setAttribute("searchErrorMSg","请输入关键词");
        return "jsp/catalog/main";

    }

    @GetMapping("/productAuto")
    public void productAuto(String keyword, Model model, HttpServletResponse resp) throws IOException {
        List<Product> productList = catalogService.searchProductList(keyword);
        model.addAttribute("productList", productList);
        String result = JSON.toJSONString(productList);
        System.out.println(result);

        resp.setContentType("text/json");
        PrintWriter out = resp.getWriter();
        out.println(result);

    }

    @GetMapping("/help")
    public String help(){
        return "help";
    }

}
